package com.hexanet.client;

import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.impl.client.DefaultHttpClient;

import com.hexanet.bom.Cell;
import com.hexanet.bom.Location;
import com.hexanet.bom.Player;
import com.hexanet.client.writer.HexanetWriter;

public class CellResourceClient {
    
    public void addHomeCell() throws Exception {

	
	Location loc = new Location(12D, 13D);
	
	Cell c = new Cell();
	c.setLocation(loc);
	c.setName("the cell");
	c.setGenerated(false);
	c.setHomebase(true);
	c.setLevel(null);

	DefaultHttpClient httpclient = new DefaultHttpClient();
	HexanetWriter writer = new HexanetWriter();
	HttpPut putRequest = writer.getPutRequest(ClientUtil.serverUrl
		+ ClientUtil.resPath + "/cells/createhomecell", httpclient, c);
	
	httpclient.getCredentialsProvider()
	.setCredentials(
		new AuthScope("localhost", 8443),
		new UsernamePasswordCredentials(
			"stefanstift@gmail.com", "test"));


	HttpResponse response = null;
	response = httpclient.execute(putRequest);

	StatusLine line = response.getStatusLine();
	System.out.println(line.getStatusCode() + " - "
		+ line.getReasonPhrase());

	// return code indicates upload failed so throw exception
	if (line.getStatusCode() < 200 || line.getStatusCode() >= 300) {
	    throw new Exception("PUT failed");
	}

	httpclient.getConnectionManager().shutdown();
    }

}
